// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.vehicles;

import gnu.trove.map.hash.TIntObjectHashMap;
import gnu.trove.map.hash.TShortObjectHashMap;
import java.util.LinkedList;
import java.util.List;

public final class VehicleCache {
    public short id;
    float x;
    float y;
    float z;
    private static TShortObjectHashMap<VehicleCache> mapId = new TShortObjectHashMap<>();
    private static TIntObjectHashMap<List<VehicleCache>> mapXY = new TIntObjectHashMap<>();

    public static void vehicleUpdate(short _id, float ax, float ay, float az) {
        VehicleCache vehicleCache0 = mapId.get(_id);
        if (vehicleCache0 != null) {
            int int0 = (int)(vehicleCache0.x / 10.0F);
            int int1 = (int)(vehicleCache0.y / 10.0F);
            int int2 = (int)(ax / 10.0F);
            int int3 = (int)(ay / 10.0F);
            if (int0 != int2 || int1 != int3) {
                mapXY.get(int0 * 65536 + int1).remove(vehicleCache0);
                if (mapXY.get(int2 * 65536 + int3) == null) {
                    mapXY.put(int2 * 65536 + int3, new LinkedList());
                }

                mapXY.get(int2 * 65536 + int3).add(vehicleCache0);
            }

            vehicleCache0.x = ax;
            vehicleCache0.y = ay;
            vehicleCache0.z = az;
        } else {
            VehicleCache vehicleCache1 = new VehicleCache();
            vehicleCache1.id = _id;
            vehicleCache1.x = ax;
            vehicleCache1.y = ay;
            vehicleCache1.z = az;
            mapId.put(_id, vehicleCache1);
            int int4 = (int)(ax / 10.0F);
            int int5 = (int)(ay / 10.0F);
            if (mapXY.get(int4 * 65536 + int5) == null) {
                mapXY.put(int4 * 65536 + int5, new LinkedList());
            }

            mapXY.get(int4 * 65536 + int5).add(vehicleCache1);
        }
    }

    public static List<VehicleCache> vehicleGet(float ax, float ay) {
        int int0 = (int)(ax / 10.0F);
        int int1 = (int)(ay / 10.0F);
        return mapXY.get(int0 * 65536 + int1);
    }

    public static List<VehicleCache> vehicleGet(int wx, int wy) {
        return mapXY.get(wx * 65536 + wy);
    }

    public static void remove(short _id) {
        VehicleCache vehicleCache = mapId.get(_id);
        if (vehicleCache != null) {
            mapId.remove(_id);
            int int0 = (int)(vehicleCache.x / 10.0F);
            int int1 = (int)(vehicleCache.y / 10.0F);
            int int2 = int0 * 65536 + int1;

            assert mapXY.containsKey(int2);

            assert mapXY.get(int2).contains(vehicleCache);

            mapXY.get(int2).remove(vehicleCache);
        }
    }

    public static void Reset() {
        mapId.clear();
        mapXY.clear();
    }

    static {
        mapId.setAutoCompactionFactor(0.0F);
        mapXY.setAutoCompactionFactor(0.0F);
    }
}
